<?php
include("model/places.php");
class placesController{
	
	function indexAction(){
		$smarty = $_SESSION['smarty'];
		
		$list_places_box = $this->getListPlaceBox();
		$smarty->assign("list_places_box",$list_places_box);
		
		$content = $smarty->fetch("places/places.html");		
		$smarty->assign("content", $content);
		
		$smarty->display("homepage.html");
	}
	
	function getListPlaceBox(){
		$smarty 	= $_SESSION['smarty'];
		$place_obj 	= new places();
		$placeList	= $place_obj->getPlaceList();		
		$smarty->assign("placeList", $placeList);
		$html 		= $smarty->fetch("places/places_list.html");
		return $html;
	}
	
	function insertAction(){
		$places_obj = new places();
		$data 		= array();
		$data['pla_name'] = $_REQUEST['pla_name'];
		$data['pla_address'] = $_REQUEST['pla_address'];
		$data['pla_type'] = $_REQUEST['pla_type'];
		
		
		$result = $places_obj->insertAPlace($data);
		if ($result == 1)
			$msg = "insert successfully";
		else if ($result == -1)
			$msg = "duplicated!";
		else
			$msg = "insert failed.";
			
		$smarty = $_SESSION['smarty'];		
		$smarty->assign("msg", $msg);
		$content = $smarty->fetch("places/places_insertResult.html");		
		$smarty->assign("content", $content);		
		$smarty->display("homepage.html");
		
	}
	
	function inactiveAction(){
		$pla_id = $_REQUEST['args'];
		$places_obj = new places();
		$result = $places_obj->inactivateAPlace($pla_id);
		if ($result == 1)
			$msg = "inactivate successfully";		
		else
			$msg = "inactivate failed.";
			
		$smarty = $_SESSION['smarty'];		
		
		
		$list_places_box = $this->getListPlaceBox();
		$smarty->assign("list_places_box",$list_places_box);
		$smarty->assign("msg", $msg);
		
		$content = $smarty->fetch("places/places.html");		
		$smarty->assign("content", $content);		
		$smarty->display("homepage.html");
	}
	
	function activeAction(){
		$pla_id 	= $_REQUEST['args'];
		$places_obj = new places();
		$result = $places_obj->activateAPlace($pla_id);
		if ($result == 1)
			$msg = "activate successfully";		
		else
			$msg = "activate failed.";
			
		$smarty = $_SESSION['smarty'];		
		
		
		$list_places_box = $this->getListPlaceBox();
		$smarty->assign("list_places_box",$list_places_box);
		$smarty->assign("msg", $msg);
		
		$content = $smarty->fetch("places/places.html");		
		$smarty->assign("content", $content);		
		$smarty->display("homepage.html");
	}
	
	function insertFormAction(){
		$smarty = $_SESSION['smarty'];		
		
		$insertform_places_box = $smarty->fetch("places/places_insert_form.html");
		$smarty->assign("insertform_places_box",$insertform_places_box);
		
		$content = $smarty->fetch("places/places.html");		
		$smarty->assign("content", $content);
		
		$smarty->display("homepage.html");
	}
	
	function editFormAction(){
		$pla_id 			= $_REQUEST['args'];
		$smarty 			= $_SESSION['smarty'];		
		
		$places_obj 		= new places();
		$current_data 		= array();
		$current_data 		= $places_obj->getPlacesInfo($pla_id);
		
		$smarty->assign("current_data", $current_data);
		
		$editform_places_box = $smarty->fetch("places/places_edit_form.html");
		$smarty->assign("editform_places_box",$editform_places_box);
		
		$content = $smarty->fetch("places/places.html");		
		$smarty->assign("content", $content);
		
		$smarty->display("homepage.html");
		
	}
	
	function editAction(){
		$places_obj = new places();
		$data 		= array();
		$data['pla_id'] 		= $_REQUEST['pla_id'];
		$data['pla_name'] 		= $_REQUEST['pla_name'];
		$data['pla_address'] 	= $_REQUEST['pla_address'];
		$data['pla_type'] 		= $_REQUEST['pla_type'];
		
		
		$result = $places_obj->editAPlace($data);
		
		if ($result == 1)
			$msg = "edit successfully";
		else if ($result == -1)
			$msg = "duplicated!";
		else
			$msg = "update failed.";
			
		$smarty = $_SESSION['smarty'];		
		$smarty->assign("msg", $msg);
		$content = $smarty->fetch("places/places_updateResult.html");		
		$smarty->assign("content", $content);		
		$smarty->display("homepage.html");
	}
		
}
?>